package lbframework.gui;

import com.sun.awt.AWTUtilities;
import java.awt.ComponentOrientation;
import lbframework.bean.dialog.LBDialog;
import lbframework.classe.OperacaoCanceladaException;
import lbframework.classe.Util;

public class ProgressBarGUI extends LBDialog {

    private boolean cancel = false;
    private boolean pause = false;

    public ProgressBarGUI() throws Exception {
        initComponents();

        centralizarForm();
        this.setResizable(false);
        this.setPause(false);

        vRToolBar1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

        lblStatus.setText("");
        prgStatus.setValue(0);

        if (!Util.getRoot().equals("/")) {
            AWTUtilities.setWindowOpacity(this, 0.92F);
        }

        this.setModal(true);
    }

    public void setMaximum(int n) throws Exception {
        prgStatus.setMaximum(n);
    }

    public void next() throws Exception {
        if (this.cancel) {
            throw new OperacaoCanceladaException();
        }

        while (this.pause) {
            Thread.sleep(100);
        }

        prgStatus.setValue(prgStatus.getValue() + 1);
    }

    public void setStatus(String s) throws Exception {
        lblStatus.setText(s);
    }

    public void clear() throws Exception {
        prgStatus.setValue(0);
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        vRPanel1 = new lbframework.bean.panel.LBPanel();
        lblStatus = new lbframework.bean.label.LBLabel();
        prgStatus = new lbframework.bean.progressBar.LBProgressBar();
        vRToolBar1 = new lbframework.bean.toolBar.LBToolBar();
        btnPause = new lbframework.bean.button.LBButton();
        btnCancelar = new lbframework.bean.button.LBButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Executando...");
        setUndecorated(true);

        vRPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        lblStatus.setText("Mensagem");

        vRToolBar1.setRollover(true);

        btnPause.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/pausa.png"))); // NOI18N
        btnPause.setToolTipText("Pausar");
        btnPause.setFocusable(false);
        btnPause.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnPause.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnPause.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnaPausarIniciarActionPerformed(evt);
            }
        });
        vRToolBar1.add(btnPause);

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/cancelar.png"))); // NOI18N
        btnCancelar.setToolTipText("Cancelar");
        btnCancelar.setFocusable(false);
        btnCancelar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnCancelar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        vRToolBar1.add(btnCancelar);

        javax.swing.GroupLayout vRPanel1Layout = new javax.swing.GroupLayout(vRPanel1);
        vRPanel1.setLayout(vRPanel1Layout);
        vRPanel1Layout.setHorizontalGroup(
            vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(vRPanel1Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(prgStatus, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, vRPanel1Layout.createSequentialGroup()
                        .addComponent(lblStatus, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(vRToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 59, Short.MAX_VALUE)))
                .addGap(21, 21, 21))
        );
        vRPanel1Layout.setVerticalGroup(
            vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(vRPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblStatus, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                    .addComponent(vRToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(prgStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(25, 25, 25))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(vRPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(vRPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        try {
            Util.exibirMensagemConfirmar("Confirma o cancelamento?", "Executando...");

            this.cancel = true;
            setStatus("Cancelando...");

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, "Atenção");
        }
}//GEN-LAST:event_btnCancelarActionPerformed

    private void btnaPausarIniciarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnaPausarIniciarActionPerformed
        try {
            if (pause) {
                btnPause.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/pausa.png")));
                btnPause.setToolTipText("Pausar");
                this.setCancel(true);
                this.pause = false;

            } else {
                btnPause.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/iniciar.png")));
                btnPause.setToolTipText("Iniciar");
                this.setCancel(false);
                this.pause = true;
            }

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, getTitle());
        }
    }//GEN-LAST:event_btnaPausarIniciarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private lbframework.bean.button.LBButton btnCancelar;
    private lbframework.bean.button.LBButton btnPause;
    private lbframework.bean.label.LBLabel lblStatus;
    private lbframework.bean.progressBar.LBProgressBar prgStatus;
    private lbframework.bean.panel.LBPanel vRPanel1;
    private lbframework.bean.toolBar.LBToolBar vRToolBar1;
    // End of variables declaration//GEN-END:variables

    public void setCancel(boolean aCancel) {
        btnCancelar.setVisible(aCancel);
    }

    public void setPause(boolean aPause) {
        btnPause.setVisible(aPause);
    }

    public void setIndeterminate(boolean aIndeterminate) {
        prgStatus.setIndeterminate(aIndeterminate);
    }
}
